package acceptTests;

import java.io.IOException;
import java.util.Iterator;

import exceptions.database.InvalidUserListException;

import Search.SearchFacade;
import sessionManager.SessionManager;
import sessionManager.SessionManagerIF;
import user.UserIF;
import user.blog.BlogIF;


public class US12Facade {

	private SessionManagerIF session ;
    private SearchFacade SF;
	
	public US12Facade(){
		session = SessionManager.getInstance();
		SF = new SearchFacade();
		
	}
	
	public void loadData() throws Exception {
		session.loadData();
	}
	
	public String logon(String login, String senha) throws Exception {
		return session.loginUser(login, senha);
	}

	public boolean logoff(String sessionID) throws Exception {
		return session.logoffUser(sessionID);
	}
	
	public void cleanPersistence(){
		session.cleanPersistence();
	
	}
	
	public void createProfile(String login, String senha, String nome_exibicao,
			String email, String sexo, String dataNasc, String endereco,
			String interesses, String quem_sou_eu, String filmes,
			String musicas, String livros) throws Exception {
		
		session.createUser(login, senha, nome_exibicao, email, sexo, dataNasc,
				endereco, interesses, quem_sou_eu, filmes, musicas, livros);
		
	}
	
	public String createBlog(String idSession, String title, String description)
	throws Exception {

		return session.getUser(idSession).createBlog(idSession, title, description);
	}
	
	public String createPost(String idSession, String blogId, String title,
			String content) throws Exception {
		
		return session.getUser(idSession).getBlog(blogId).addPost(title, content);
	}
	
	public String addComment(String sessionID, String postID, String text) throws Exception{
		Iterator<UserIF> itUsers = session.getUsers().iterator();

		while (itUsers.hasNext()) {
			UserIF tmpUser = itUsers.next();
			Iterator<BlogIF> itBlog = tmpUser.getBlogs().iterator();
			while(itBlog.hasNext()) {
				BlogIF tmpBlog = itBlog.next();
				if(tmpBlog.getPostManager().find(postID) != null) {
					return tmpBlog.getPostManager().find(postID).addComment(
							session.getUser(sessionID).getLogin(), text);
				}
			}
		}
		return null;
	}
	
	
	public String findProfileByName(String name){
		return SF.findProfileByName(name);
		
	}
	
	public String findProfileByInterests(String interest){
		return SF.findProfileByInterests(interest);
	}
	
	public String findProfileByGender(String gender) throws Exception {		
		return SF.findProfileByGender(gender);
	}
	
	public String findBlogByName(String name){
		return SF.findBlogByName(name);
	}
	
	public void saveData() throws InvalidUserListException, IOException {
		session.saveData();
	}	
	public Integer getNumberOfComments(String login, String blogId) {
		return SF.getNumberOfComments(login, blogId);
	}
}
